/*
 * author M.Chang
 *
 * Copyright 2008 Ph.Bonnet and M.Chang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

#include "Tokenize.h"


/*****************************************************************************/

/* constructor */
Tokenize::Tokenize()
{
}

/* destructor */
Tokenize::~Tokenize()
{
}


//http://www.linuxselfhelp.com/HOWTO/C++Programming-HOWTO-7.html

void Tokenize::Tokens(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters)
{
    /* Skip delimiters at beginning. */
    std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);

    /* Find first "non-delimiter". */
    std::string::size_type pos     = str.find_first_of(delimiters, lastPos);

    while (std::string::npos != pos && std::string::npos != lastPos)
    {
        /* Found a token, add it to the vector. */
        tokens.push_back(str.substr(lastPos, pos - lastPos));

        /* Skip delimiters.  Note the "not_of" */
        lastPos = str.find_first_not_of(delimiters, pos);
        /* Find next "non-delimiter" */
        pos = str.find_first_of(delimiters, lastPos);
    }

    if (std::string::npos != lastPos)
    {
      /* add last token to the vector. */
      tokens.push_back(str.substr(lastPos));
    }
}


void Tokenize::Tokens(const std::string& str, std::vector<std::string>& tokens, const std::string& tag_start, const std::string& tag_end)
{
    // Skip delimiters at beginning.
    std::string::size_type lastPos = str.find(tag_start, 0);

    // Find first "non-delimiter".
    std::string::size_type pos     = str.find(tag_end, lastPos);

    while (std::string::npos != pos && std::string::npos != lastPos)
    {
        lastPos += tag_start.size();

        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));

        // Skip delimiters.
        lastPos = str.find(tag_start, pos);
        // Find next "non-delimiter"
        pos = str.find(tag_end, lastPos);

    }
}

